/*global pi, log */

pi.mod.define('pi.util', function (pi) {
	"use strict";
	var module = {};

	/* 遍历所有属性和方法 */
	module.travers = function (obj) {
		var s, p, funs = "",
			prop = "";

		for (p in obj) {
			if (typeof (obj[p]) === "function") {
				funs += p + ", ";
			} else {
				prop += p + " = " + obj[p] + "\n";
			}
		}
		s = "\nProperties======================:\n" + prop + "\n\nFunctions===================:\n" + funs;
		pi.log(s);
		return s;
	};

	//如果undefined,则设置为default value
	module.defaultValue = module.DV = function (v, def) {
		var r;
		if (v !== undefined) {
			r = v;
		} else {
			r = def;
		}
		return r;
	};

	//val为负数,则将其转换为相应的正数
	module.toUint = function (val) {
		if (val >= -0x80000000 && val <= 0x7fffffff) {
			var a = 0x100000000; //不能用1<<32
			val = (val + a) % a;
			return val;
		}
	};

	//用二分查找排序数组，comp函数必须返回0 表示等于，-1表示小于，1表示大于，默认升序，descending 表示是否为降序，
	module.orderFind = function (obj, arr, comp, descending) {
		var r, i, j = 0,
		k = arr.length;
		if(descending){
			while (j < k) {
				i = (j + k) >> 1;
				r = compare(obj, arr[i]);
				if (r > 0) {
					k = i;
				} else if (r < 0) {
					j = i + 1;
				} else {
					return i;
				}
			}
			return - 1;
		}
		while (j < k) {
			i = (j + k) >> 1;
			r = compare(obj, arr[i]);
			if (r > 0) {
				j = i + 1;
			} else if (r < 0) {
				k = i;
			} else {
				return i;
			}
		}
		return - 1;
	};

	return module;
});